package com.xuecheng.checkcode.controller;

import com.xuecheng.checkcode.context.CheckCodeStrategyContext;
import com.xuecheng.checkcode.enums.CheckCodeType;
import com.xuecheng.checkcode.model.GenerateCheckCodeParamsDTO;
import com.xuecheng.checkcode.model.CheckCodeResultDTO;
import com.xuecheng.checkcode.service.CheckCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 勾新杰
 * @version 1.0
 * @description 验证码服务接口
 * @date 2022/9/29 18:39
 */
@Api(value = "验证码服务接口", tags = "验证码服务接口")
@RestController
@RequiredArgsConstructor
public class CheckCodeController {

    /**
     * 验证码服务上下文，用于选择哪一种验证码服务，工厂加策略
     */
    private final CheckCodeStrategyContext checkCodeStrategyContext;

    /**
     * 生成验证信息
     *
     * @param generateCheckCodeParamsDTO 生成验证信息请求参数
     * @return CheckCodeResultDto
     */
    @ApiOperation(value = "生成验证信息", notes = "生成验证信息")
    @PostMapping(value = "/pic")
    public CheckCodeResultDTO generatePicCheckCode(GenerateCheckCodeParamsDTO generateCheckCodeParamsDTO) {
        return checkCodeStrategyContext.generate(CheckCodeType.PIC, generateCheckCodeParamsDTO);
    }

    /**
     * 校验验证码
     *
     * @param key  验证key
     * @param code 验证码
     * @return 校验结果
     */
    @ApiOperation(value = "校验验证码是否有效", notes = "校验")
    @ApiImplicitParams({ // 参数列表
            @ApiImplicitParam(name = "name", value = "业务名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "key", value = "验证key", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping(value = "/verify")
    public Boolean verify(String key, String code) {
        return checkCodeStrategyContext.verify(key, code);
    }

    /**
     * 生成手机验证码
     *
     * @param generateCheckCodeParamsDTO 生成验证信息请求参数
     */
    @PostMapping(value = "/phone")
    @ApiOperation(value = "生成手机验证码")
    public CheckCodeResultDTO generatePhoneCheckCode(GenerateCheckCodeParamsDTO generateCheckCodeParamsDTO) {
        return checkCodeStrategyContext.generate(CheckCodeType.SMS, generateCheckCodeParamsDTO);
    }

    /**
     * 生成邮箱验证码
     *
     * @param generateCheckCodeParamsDTO 生成验证信息请求参数
     */
    @PostMapping(value = "/email")
    @ApiOperation(value = "生成邮箱验证码")
    public CheckCodeResultDTO generateEmailCheckCode(GenerateCheckCodeParamsDTO generateCheckCodeParamsDTO) {
        return checkCodeStrategyContext.generate(CheckCodeType.EMAIL, generateCheckCodeParamsDTO);
    }
}
